<?php
$section = "user_admin";

$titre = _LB_USER_MANAGEMENT;
$smarty->assign('titre', $titre);

$annonceur_id = Request::getIntNumber('annonceur_id');
$account_type = Request::getIntNumber("account_type");
$is_regie = Request::getIntNumber("is_regie");

$act = Request::getIntNumber('act');
$backlink = Session::getbacklink($section,"list/?");

$annonceur = new Login();

if ($is_regie == ADMIN_SUPPORT)
	$account_type = ACCOUNT_TYPE_SUPPORT;

if ($annonceur_id > 0 && $annonceur->get($annonceur_id) == false)
	redirect($backlink);

if (isset($annonceur->IS_REGIE) && $annonceur->IS_REGIE == 2 && $_SESSION["userClasse"] != "user_support"){
    redirect($backlink);
}

if ($act == ACT_VALID_EDIT)
{
	if ( $account_type==ACCOUNT_TYPE_SUPPORT)
	{
		$pass = Request::get('PASS');
		$pass_confirm = Request::get('PASS_CONFIRM');

		$annonceur->EMAIL = Request::get("EMAIL");
		$annonceur->NOM = Request::get("NOM");
		$annonceur->PRENOM = Request::get("PRENOM");
		$annonceur->FAX = Request::get("FAX");
		$annonceur->CODE = Request::get("CODE");
		$annonceur->FONCTION = Request::get("FONCTION");

		if ($annonceur->ID==0)
		{
			$annonceur->ID_ANNONCEUR = $annonceur_id;
			$annonceur->PASS = sha1(Request::get("PASS"));
			$annonceur->IS_REGIE = $is_regie;
		}

		if (!checkEmailValide($annonceur->EMAIL))
		{
			$error_msg->add(_LB_INVALID_EMAIL);
		}
		else if (Login::emailExistByLoginID($annonceur->EMAIL, $annonceur->ID))
		{
			$error_msg->add(_LB_INVALID_EMAIL);
		}
		else if (!Utils::isValidLength($annonceur->EMAIL, MAX_LENGTH_NAME))
		{
			$error_msg->add(_MSG_EMAIL_IS_TOO_LONG);
		}

		if ($annonceur->ID == 0)
		{
			if (trim($pass) == "" || trim($pass_confirm) == "")
			{
				$error_msg->add(_LB_INVALID_PASSWORD);
			}
			else if (!Utils::isValidLength($pass, MAX_LENGTH_PASSWORD))
			{
				$error_msg->add(_MSG_PASSWORD_IS_TOO_LONG);
			}
			else if (!Utils::isValidMinLength($pass, MIN_LENGTH_PASSWORD))
			{
				$error_msg->add(_MSG_PASSWORD_IS_TOO_SHORT);
			}
			else if ($pass != $pass_confirm)
			{
				$error_msg->add(_LB_PASSWORD_DOESNT_MATCH);
			}
		}

		if (trim($annonceur->NOM) == "")
		{
			$error_msg->add(_LB_NAME_IS_EMPTY);
		}
		else if (!Utils::isValidLength($annonceur->NOM, MAX_LENGTH_NAME))
		{
			$error_msg->add(_LB_INVALID_LENGTH_NOM);
		}
		if (!Utils::isValidLength($annonceur->PRENOM, MAX_LENGTH_NAME))
		{
			$error_msg->add(_LB_PRENOM_IS_TOO_LONG);
		}

		// check whether or not Code exist
		if (strlen($annonceur->CODE) != MAX_LENGTH_USER_CODE)
		{
			$error_msg->add(_MSG_CODE_IS_INVALID);
		}
		else if (preg_match("/[a-z0-9A-Z]{3}/", $annonceur->CODE, $matches) != 1)
		{
			$error_msg->add(_MSG_CODE_IS_INVALID);
		}
		else if (Login::codeExistByLoginID($annonceur->CODE, $annonceur->ID))
		{
			$error_msg->add(_MSG_CODE_EXISTS);
		}

		if (!Utils::isValidLength($annonceur->FAX, MAX_LENGTH_NAME))
		{
			$error_msg->add(_MSG_FAX_IS_TOO_LONG);
		}
		if (!Utils::isValidLength($annonceur->FONCTION, MAX_LENGTH_NAME))
		{
			$error_msg->add(_LB_FONCTION_IS_TOO_LONG);
		}
		if (!sizeof($error_msg->errors))
		{
			$annonceur->store();
			redirect($backlink);
		}

	}

}
elseif ($act== ACT_DELETE)
{
	if($annonceur->canDelete())
	{
		$annonceur->DISABLED = 1;
		$annonceur->NOM = $annonceur->ID . $annonceur->NOM;
		$annonceur->store();
	}

	redirect($backlink);
}

// create head name
if ( $account_type==ACCOUNT_TYPE_SUPPORT)
{
    if ($annonceur->ID){
        $head_name = _LB_EDIT_SUPPORT;
        $head_name_box = _LB_SUPPORT_ACCOUNT;
    }
	else{
        $head_name = _LB_ADD_SUPPORT;
        $head_name_box = _LB_SUPPORT_ACCOUNT;
	}
}

// create Cfer
$cfer = new Cfer(array(
    _LB_ACCUEIL => Utils::getHomeLink(),
    _LB_USER_MANAGEMENT => APPLICATION_URL . '/user_admin/list/?',
    $head_name => Utils::getCurrentLink()
));

$smarty->assign("annonceur", $annonceur);
$smarty->assign("account_type", $account_type);
$smarty->assign("act", $act);
$smarty->assign("backlink",$backlink);
$smarty->assign("cfer",$cfer);
$smarty->assign("lb_head",$head_name);
$smarty->assign("lb_head_box",$head_name_box);
$smarty->display("user_admin_form.html");